package mvc.views.frames
{
	import alternativa.gui.layout.LayoutManager;
	import alternativa.gui.theme.defaulttheme.container.panel.Panel;
	import alternativa.gui.theme.defaulttheme.container.scrollPane.ScrollPaneWithScrollBar;
	
	import com.greensock.TweenLite;
	import com.greensock.TweenNano;
	import com.greensock.easing.Back;
	
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import globals.Config;
	import globals.Global;
	
	import interfaces.ILayoutAnimationFrame;
	
	import mvc.views.components.main.ContentBase;
	import mvc.views.components.main.FactoryContent;
	
	import mx.core.IAssetLayoutFeatures;
	
	import utils.UtilsUIManager;

	public class FrameMain extends Frame implements ILayoutAnimationFrame
	{
		private var _holderPanel:Panel;
		
		private var _scrollContent:ScrollPaneWithScrollBar;
		
		private var _content:ContentBase;
		
		private var _maskShape:Shape;
		
		private var _nextContent:ContentBase;
		
		private var _model:Object=null;
		
		public function FrameMain()
		{
			super();
		}
		
		public function changeMain($model:Object):void
		{
			_model = $model;
			var content:ContentBase = FactoryContent.createContent(_model);
			content.resize(animationWidth-20,animationHeight-50);
			content.update(null);
			if(!_content)		//第一次显示内容
			{
				_content = content
				_scrollContent.content = _content;
			}
			else
			{
				_nextContent = content;
			}			
		}
		
		override protected function initUI():void
		{
			// TODO Auto Generated method stub
			super.initUI();
			
			_holderPanel = UtilsUIManager.createPanel(this);
			_scrollContent = new ScrollPaneWithScrollBar();
			_holderPanel.content = _scrollContent;
			
			_maskShape = new Shape();
			
			_scrollContent.mask = _maskShape;
			_scrollContent.addChild(_maskShape);
			resize();
		}
		
		override public function resize(evt:Event=null):void
		{
			// TODO Auto Generated method stub
			super.resize(evt);
			this.x = positionX;
			this.y = positionY;
			
			_holderPanel.resize(animationWidth,animationHeight);
			_scrollContent.resize(animationWidth-20,animationHeight-50);
			var gra:Graphics = _maskShape.graphics;
			gra.beginFill(0);
			gra.drawRect(0,0,animationWidth-20,animationHeight-50);
			gra.endFill();
		}
		

		override public function update($model:Object):void
		{
			// TODO Auto Generated method stub
			super.update($model);
			
			
			_content.update($model);
			
		}

		/**
		 * 如果没有初始化,则初始化并且使用第一种显示方式
		 */
		override public function show():void
		{
			// TODO Auto Generated method stub
			super.show();
			if(!_nextContent)
			{
				_holderPanel.alpha = 0;
				_holderPanel.y = 50;
				TweenLite.to(_holderPanel,1,{y:0,alpha:1,onStart:function():void{
					
					if(_scrollContent.scrollBar.visible)
						TweenNano.to(_scrollContent.scrollBar,0.5,{alpha:0});
					
				}});
			}
			else
			{
				_nextContent.x = _content.width;
				_nextContent.alpha = 0;
				_scrollContent.addChild(_nextContent);
				TweenLite.to(_content,0.5,{x:-_content.width,ease:Back.easeOut,onStart:function():void{
					
					if(_scrollContent.scrollBar.visible)
						TweenNano.to(_scrollContent.scrollBar,0.5,{alpha:0});
					
				}});
				TweenLite.to(_nextContent,0.5,{x:0,alpha:1,ease:Back.easeOut,onComplete:function():void{
					if(_scrollContent.scrollBar.visible)
						TweenNano.to(_scrollContent.scrollBar,0.5,{alpha:1});
					
					if(_nextContent)
					{
						_scrollContent.content = _nextContent;
						_content = _nextContent;
						_nextContent = null;				
					}

				}});
			}
		}
		
		private function switchContent():void
		{
			
		}
		
		public function get animationHeight():int
		{
			// TODO Auto Generated method stub
			return Global.stageH - Config.bottomHeight - Config.headerHeight;
		}
		
		public function get animationWidth():int
		{
			// TODO Auto Generated method stub
			return Global.stageW - positionX-5;
		}
		
		public function endAnimation():void
		{
			// TODO Auto Generated method stub
			
		}
		
		public function get holderPanel():Panel
		{
			// TODO Auto Generated method stub
			return _holderPanel;
		}
		
		public function get positionX():int
		{
			// TODO Auto Generated method stub
			return Config.leftPanelWidth+10;
		}
		
		public function get positionY():int
		{
			// TODO Auto Generated method stub
			return Config.headerHeight;
		}
		
		public function startAnimation():void
		{
			// TODO Auto Generated method stub
			
		}
		
		override public function reset():void
		{
			// TODO Auto Generated method stub
			super.reset();
			_content = _nextContent = null;
		}
		
	}
}